package net.topicopedia.servlets.categories;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class CreateCategoriesServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		      throws IOException {
		
		resp.addHeader("Access-Control-Allow-Origin", "*");
		resp.setContentType("text/plain");
		
		List<String> list = new ArrayList<String>();
		list.add("Arts and Literature");
		list.add("Social Science");
		list.add("Geography");
		list.add("Human Body and Health");
		list.add("Biosphere");
		list.add("Sciences and Mathematics");
		list.add("Sports");
		list.add("Technology");
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		int keyId = 1000;
		
		for ( String string : list )
		{
			keyId++;
			Entity category = new Entity("Category");
			category.setProperty("name", string);
			category.setProperty("count", 0);
			category.setProperty("tags", null);
			category.setProperty( Entity.KEY_RESERVED_PROPERTY, KeyFactory.createKey("Category", keyId ) );
			datastore.put( category );
		}
		
		resp.getWriter().println( "done" );
	}

}
 